package com.boot2551.service;


import  com.boot2551.bean.Emp;
import com.boot2551.mapper.EmpMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
@Service
public class EmpS  implements EmpMapper {
    @Resource
    public EmpMapper em;
    @Override
    public int insert(Emp e) {
        return em.insert(e);
    }
   public void copy(String path,Emp e) throws IOException {
      InputStream is=e.getEpic2();
      OutputStream os=new FileOutputStream(path+"/"+e.getEpic());
       byte d[]=new byte[1024];
       int len=is.read(d);
       while(len!=-1){
           os.write(d,0,len);
           len=is.read(d);
       }
       is.close();
       os.close();
   }
    /**
     * 将数据装入集合，同时将图片复制到服务器路径
     * @param path
     * @return
     */
    @Override
    public List<Emp> all(String path) throws IOException {
        List<Emp> data=em.all(path);
        for(Emp e:data){
            if (e.getEpic2()!=null&&!e.getEpic().equals("")){
//                复制
                copy(path,e);
            }
            //清空二进制
            e.setEpic2(null);
        }
        return data;
    }

    @Override
    public int delete(int id) {
        return em.delete(id);
    }

    @Override
    public Emp one(int id, String path) throws IOException {
        Emp data=em.one(id,path);
        if (data.getEpic2()!=null&&!data.getEpic().equals(""))
         copy(path,data);
        data.setEpic2(null);
        return data;
    }

    @Override
    public int update(Emp e) {
        return em.update(e);
    }

    @Override
    public int updateselective(Emp e) {
        return em.updateselective(e);
    }
}
